package com.meepo.feed.comment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.cardview.widget.CardView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.meepo.basic.router.RouterManager
import com.meepo.basic.components.listfragment.MeepoListFragment
import com.meepo.feed.API
import com.meepo.feed.R
import com.meepo.basic.schema.Enums
import com.meepo.basic.schema.common.PageResult
import com.meepo.basic.schema.common.ResponseWrapper
import com.meepo.basic.schema.feed.Comment
import com.meepo.feed.API.Companion.SearchType_Main
import com.meepo.framework.design.imageview.CircularImage
import com.meepo.framework.design.recycler.LinearAdapter
import com.meepo.framework.design.recycler.ViewHolder
import com.meepo.framework.design.tag.BasicTag
import com.meepo.framework.design.tag.TagView
import com.xiaojinzi.component.support.ParameterSupport
import io.reactivex.Observable

/**
 * @author
 *
 * @date  11/14/20 7:55 PM
 * @version 1.0
 */
class CommentMainListFragment : MeepoListFragment<Comment>() {

    companion object {
        fun instance(itemId: String = ""): CommentMainListFragment {
            val rsp = CommentMainListFragment()
            val bundle = Bundle()
            bundle.putString("mItemId", itemId)
            rsp.arguments = bundle
            return rsp
        }
    }


    override fun loadDataObservable(page: Int): Observable<ResponseWrapper<PageResult<Comment>>> {
        val mItemId = ParameterSupport.getString(arguments, "mItemId") ?: ""

        return API.instance().fetchMainComments(targetId = mItemId, search = SearchType_Main, page = page)
    }

    @SuppressLint("SetTextI18n")
    override fun bind(item: Comment, holder: ViewHolder, position: Int) {

        // 头像
        holder.getView<CircularImage>(R.id.avatar_ci)?.let {
            holder.setImagePath(R.id.avatar_ci, item.author?.avatar ?: "")
            it.setOnClickListener {
                item.author?.id?.let { userId ->
                    RouterManager.UserModule.routeToUserProfile(requireContext(), userId)
                }
            }
        }

        // 昵称
        holder.getView<TextView>(R.id.username_tv)?.let {
            it.text = item.author?.nickname ?: ""
            it.setOnClickListener {
                item.author?.id?.let { userId ->
                    RouterManager.UserModule.routeToUserProfile(requireContext(), userId)
                }
            }
        }

        holder.getView<TagView>(R.id.tag_tv)?.let {
            item.author?.getTag()?.let { tag ->
                it.text = tag
            }

            if (item.author?.getTag() == null) {
                it.visibility = View.GONE
            }
        }

        // 喜欢
        holder.setText(R.id.like_count_tv, "" + item.getLikeCount())

        val likeColor = requireContext().resources.getColor(R.color.like)
        val normalColor = requireContext().resources.getColor(R.color.primary_text_color)

        holder.getView<ImageView>(R.id.like_iv)?.let { imageView ->
            if (item.isLiked()) {
                imageView.setImageResource(R.drawable.ic_like)
                imageView.setColorFilter(likeColor)
            } else {
                imageView.setImageResource(R.drawable.ic_like_line)
                imageView.setColorFilter(normalColor)
            }
        }

        holder.getView<LinearLayout>(R.id.like_ll)?.setOnClickListener {
            presenter.call(
                API.instance()
                    .addAction(item.id, Enums.ITEM_TYPE_COMMENT, Enums.ACTION_TYPE_LIKED, cancel = if (item.isLiked()) 1 else 0)
            )

            this.replace(position, item.switchLiked())
        }

        holder.getView<TextView>(R.id.like_count_tv)?.let {
            it.text = "" + item.getLikeCount()
            it.setTextColor(if (item.isLiked()) likeColor else normalColor)
        }

        // 评论内容
        holder.setText(R.id.content_tv, item.content ?: "")


        // 回复
        holder.getView<BasicTag>(R.id.reply_tag)?.let {
            it.setOnClickListener {
                RouterManager.FeedModule.routeToFeedCommentReply(requireContext(), item.id)
            }
        }

        holder.getView<CardView>(R.id.reply_card_view)?.let { cardView ->

            if (item.replies == null || item.replies!!.isEmpty()) {
                cardView.visibility = View.GONE
            } else {
                cardView.visibility = View.VISIBLE
            }
        }

        holder.getView<RecyclerView>(R.id.reply_recycler_view)?.let { rv ->

            item.replies?.let {
                rv.layoutManager = LinearLayoutManager(requireContext())
                rv.adapter = object : LinearAdapter<Comment>(requireContext(), it) {
                    override fun getItemLayoutId(item: Comment, position: Int): Int {
                        return R.layout.comment_reply_list_item_reply_item_layout
                    }

                    override fun bind(item: Comment, holder: ViewHolder, position: Int) {

                        holder.setImagePath(R.id.avatar_ci, item.author?.avatar)
                        holder.setText(R.id.nickname_tv, item.author?.nickname)
                        holder.setOnClickListener(listOf(R.id.avatar_ci, R.id.nickname_tv)) {
                            RouterManager.UserModule.routeToUserProfile(requireContext(), item.author?.id)
                        }

                        holder.setText(R.id.reply_content, item.content)

                    }

                }
            }


        }

        holder.getView<TextView>(R.id.all_comment_tv)?.let {
            it.setOnClickListener {
                RouterManager.FeedModule.routeToFeedCommentReply(requireContext(), item.id)
            }
        }

        // 时间
        holder.setText(R.id.time_tv, item.createdAt ?: "")
    }

    override fun getItemViewLayoutId(item: Comment, position: Int): Int = R.layout.comment_main_list_item_layout

}